<?php

class RulesController extends FController {

	//权限
	public function accessRules() {
		$config = array(
		'index' => 1,
		);
		return ZRole::auth("role", $config);
	}

	public function __common() {
		parent::init();
		if (empty($this->cLoginUser)) {
			$this->redirect($this->createUrl('/site/login'));
		}
		$this->commonScript();
		Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
	}

	/*
	* 全通格式规则
	*/

	public function actionIndex() {
		$this->__common();
		$this->title = "导出全通格式设置";
		$s['code'] = (string) $this->request->getParam('code');
		$s['name'] = (string) $this->request->getParam('name');
		$s['is_show'] = (int) $this->request->getParam('is_show');
		$s = ZTask::dis_input($s);
		$s = Func::fAddslashes($s);
		$rules = ZProblemNum::_getAllproblemrule($s);
		$pageUrl = $this->createUrl('/problem/rules/index', $s);
		$this->render('index', array(
		'search' => $s,
		'pageUrl' => $pageUrl,
		'rules' => $rules['listDatas'],
		'pages' => $rules['pages'],
		));
	}
	/*
	* 规则表 页面跳转
	*/
	public function actionAddrules() {
		$this->layout = "//layouts/main_window";
		$id = (int) $this->request->getParam('id');
		$problemrule = new Problemrule();
		$ruleinfo = $problemrule->findByPk($id);
		if ($ruleinfo) {
			$ruleinfo = $ruleinfo->attributes;
		}
		$this->render("addrules", array(
		'ruleinfo' => $ruleinfo,
		));
	}

	/*
	* 规则表 添加 跟新
	*/

	public function actionEditrules() {
		if ($_POST) {
			$_POST = ZTask::dis_input($_POST);
			if (empty($_POST['id'])) {
				$problemrule = new Problemrule();
				$_POST = ZTask::dis_input($_POST);
				$problemrule->code = strtolower($_POST['code']);
				#  验重  #
				$check = Problemrule::model()->find('"code" = \''.$problemrule->code.'\'');
				if(!empty($check)){
					$this->frameAjaxMessage(null,'代码已被占用!');
					exit;
				}

				$problemrule->name = $_POST['name'];
				$problemrule->sqlname = $_POST['sqlname'];
				$problemrule->is_exam = isset($_POST['is_exam']) ? $_POST['is_exam'] : 0;
				$problemrule->is_show = isset($_POST['is_show']) ? $_POST['is_show'] : 0;
				$problemrule->remark = $_POST['remark'];

				$problemrule->create_user = $this->cLoginUser['cuser']['id'];
				$problemrule->create_time = time();
				$problemrule->update_user = $this->cLoginUser['cuser']['id'];
				$problemrule->update_time = time();
				$problemrule->add_time = time();
				if ($problemrule->save()) {
					$this->frameAjaxMessage('/problem/rules/index');
					exit;
				}
			} else {
				$_POST = ZTask::dis_input($_POST);
				$problemrule = new Problemrule();
				$problemrule = $problemrule->findByPk($_POST['id']);

				$problemrule->code = strtolower($_POST['code']);
				#  验重  #
				$check = Problemrule::model()->find('"code" = \''.$problemrule->code.'\' AND "id" != \''.$_POST['id'].'\'');
				if(!empty($check)){
					$this->frameAjaxMessage(null,'代码已被占用!');
					exit;
				}

				$problemrule->name = $_POST['name'];
				$problemrule->sqlname = $_POST['sqlname'];
				$problemrule->is_show = isset($_POST['is_show']) ? $_POST['is_show'] : 0;
				$problemrule->is_exam = isset($_POST['is_exam']) ? $_POST['is_exam'] : 0;

				$problemrule->create_user = $this->cLoginUser['cuser']['id'];
				$problemrule->create_time = time();
				$problemrule->update_user = $this->cLoginUser['cuser']['id'];
				$problemrule->update_time = time();
				$problemrule->remark = $_POST['remark'];
				if ($problemrule->save()) {
					$this->frameAjaxMessage('/problem/rules/index');
					exit;
				}
			}
		}
	}

	//删除规则表
	public function actionDeleterules() {
		$this->__common();
		$id = (int) $this->request->getParam('id');
		$problemrule = new Problemrule();
		$info = $problemrule->findByPk($id);
		if ($info) {
			$detail = new ProblemruleDetail();
			if ($detail->deleteAll('"problemrule_id" = ' . $info->id)) {
				if ($problemrule->deleteByPk($id)) {
					echo 1;
				} else {
					echo 2;
				}
			} else {
				if ($problemrule->deleteByPk($id)) {
					echo 1;
				} else {
					echo 2;
				}
			}
		}
	}

	/*
	* 规则映射表字段
	*/

	public function actionProblemruledetail() {
		$this->__common();
		$this->title = "导出全通格式设置";
		$pb_id = $s['problemrule_id'] = (int) $this->request->getParam('problemrule_id');
		$problemruledetailinfo = ZProblemNum::_getAllproblemruledetail($s);
		$pageUrl = $this->createUrl('/problem/rules/problemruledetail', $s);
		$this->render('problemruledetail', array(
		'pb_id' => $pb_id,
		'pageUrl' => $pageUrl,
		'rulesdetail' => $problemruledetailinfo['listDatas'],
		'pages' => $problemruledetailinfo['pages'],
		));
	}

	/*
	* 详情规则添加页跳转
	*/

	public function actionAddrulesdetail() {
		$this->__common();
		$id = (int) $this->request->getParam('id');
		$pb_id = (int) $this->request->getParam('pb_id');
		$ruledetail = new ProblemruleDetail();
		$problemruledetail = $ruledetail->findByPk($id);
		if ($problemruledetail) {
			$problemruledetail = $problemruledetail->attributes;
			$pb_id = $problemruledetail['problemrule_id'];
		}
		$problemrule = new Problemrule();
		$Problemruleinfo = $problemrule->findByPk($pb_id)->attributes;
		$tableinfo = Import::_gettableinfo($Problemruleinfo['sqlname']);
		$this->renderPartial("addrulesdetail", array(
		'problemruledetail' => $problemruledetail,
		'pb_id' => $pb_id,
		'tableinfo'=>$tableinfo,
		), true);
	}

	/*
	* 跟新 新添 规则详情表
	*/

	public function actionEditrulesdetail() {
		if ($_POST) {
			$_POST = ZTask::dis_input($_POST);
			$problemruledetail = new ProblemruleDetail();
			if (empty($_POST['id'])) {
				if (empty($_POST['name']) || empty($_POST['out_name'])){
					$this->frameAjaxMessage(null,'栏位名称和导出数据不能为空!');
				}else {
					if($_POST['out_name']=='gdz'){
						if(empty( $_POST['value'])){
							$this->frameAjaxMessage(null,'导出栏位为固定值就必须填固定值!');
						}
					}
				}
				$problemruledetail->problemrule_id = $_POST['pb_id'];
				$problemruledetail->number = $_POST['number'];
				$problemruledetail->name = $_POST['name'];
				$problemruledetail->out_name = $_POST['out_name'];
				$problemruledetail->value = $_POST['value'];
				$problemruledetail->create_user = $this->cLoginUser['cuser']['id'];
				$problemruledetail->create_time = time();
			} else {
				$_POST = ZTask::dis_input($_POST);
				if (empty($_POST['name']) || empty($_POST['out_name'])){
					$this->frameAjaxMessage(null,'栏位名称和导出数据不能为空!');
				}else {
					if($_POST['out_name']=='gdz'){
						if(empty( $_POST['value'])){
							$this->frameAjaxMessage(null,'导出栏位为固定值就必须填固定值!');
						}
					}
				}
				$problemruledetail = $problemruledetail->findByPk($_POST['id']);
				$problemruledetail->problemrule_id = $_POST['pb_id'];
				$problemruledetail->name = $_POST['name'];
				$problemruledetail->number = $_POST['number'];
				$problemruledetail->out_name = $_POST['out_name'];
				$problemruledetail->value = $_POST['value'];
				$problemruledetail->update_user = $this->cLoginUser['cuser']['id'];
				$problemruledetail->update_time = time();
			}
			if ($problemruledetail->save()) {
				$this->frameAjaxMessage('/problem/rules/problemruledetail/problemrule_id/' . $problemruledetail->problemrule_id);
			}
		}
	}

	//删除规则详情表数据
	public function actionDeleteruledetail() {
		$this->__common();
		$id = (int) $this->request->getParam('id');
		$problemruledetail = new ProblemruleDetail();
		if ($problemruledetail->deleteByPk($id)) {
			echo 1;
		} else {
			echo 2;
		}
	}
	
	/**
	* @ 批量删除
	* @ yindebin
	* @ 2014-01-06 pm
	*
	*/
	public function actionDelete_all(){
		$id = Yii::app ()->request->getparam ( 'id' );
		if (! empty ( $id )) {
			$select = 'SELECT "id" FROM "{{problemrule_detail}}" WHERE "problemrule_id" IN ('.$id.')';
			$data = Yii::app()->db->createCommand($select)->queryRow();
   			if(!empty($data)){
   				echo 3;
   				exit;
   			}
			
			$delete = 'DELETE FROM "{{problemrule}}" WHERE "id" IN ('.$id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			$delete = 'DELETE FROM "{{problemrule_detail}}" WHERE "problemrule_id" IN ('.$id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			echo 1;
		}else{
			echo 2;
		}
	}
	
	/**
	* @ 批量删除
	* @ yindebin
	* @ 2014-01-06 pm
	*
	*/
	public function actionDelete_d_all(){
		$id = Yii::app ()->request->getparam ( 'id' );
		if (! empty ( $id )) {
			$delete = 'DELETE FROM "{{problemrule_detail}}" WHERE "id" IN ('.$id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			echo 1;
		}else{
			echo 2;
		}
	}

}

?>
